<template>
  <div class="table-page">
    <GiTable
      title="项目管理"
      row-key="id"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #toolbar-left>
	    <a-input v-model="queryForm.id" placeholder="请输入ID" allow-clear @change="search">
	      <template #prefix><icon-search /></template>
	    </a-input>
	    <a-input v-model="queryForm.name" placeholder="请输入项目名称" allow-clear @change="search">
	      <template #prefix><icon-search /></template>
	    </a-input>
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['task:projectInfo:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
        <a-button v-permission="['task:projectInfo:export']" @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['task:projectInfo:detail']" title="详情" @click="onDetail(record)">详情</a-link>
          <a-link v-permission="['task:projectInfo:update']" title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link
            v-permission="['task:projectInfo:delete']"
            status="danger"
            :disabled="record.disabled"
            :title="record.disabled ? '不可删除' : '删除'"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <ProjectInfoAddModal ref="ProjectInfoAddModalRef" @save-success="search" />
    <ProjectInfoDetailDrawer ref="ProjectInfoDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
  import ProjectInfoAddModal from './ProjectInfoAddModal.vue'
  import ProjectInfoDetailDrawer from './ProjectInfoDetailDrawer.vue'
  import {useDownload, useTable} from '@/hooks'
  import {useDict} from '@/hooks/app'
  import {isMobile} from '@/utils'
  import has from '@/utils/has'
  import {
        type ProjectInfoResp,
        type ProjectInfoQuery,
        deleteProjectInfo,
        exportProjectInfo,
        listProjectInfo
    } from '@/apis/task/projectInfo'
    import type {TableInstanceColumns} from '@/components/GiTable/type'

    defineOptions({name: 'ProjectInfo'})


const queryForm = reactive<ProjectInfoQuery>({
  id: undefined,
  name: undefined,
  sort: ['id,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listProjectInfo({ ...queryForm, ...page }), { immediate: true })
const columns: TableInstanceColumns[] = [
  { title: 'ID', dataIndex: 'id', slotName: 'id' },
  { title: '项目名称', dataIndex: 'name', slotName: 'name' },
  { title: '项目简介', dataIndex: 'description', slotName: 'description' },
  { title: '创建人', dataIndex: 'createUser', slotName: 'createUser' },
  { title: '创建时间', dataIndex: 'createTime', slotName: 'createTime' },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 160,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['task:projectInfo:detail', 'task:projectInfo:update', 'task:projectInfo:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.id = undefined
  queryForm.name = undefined
  search()
}

// 删除
const onDelete = (record: ProjectInfoResp) => {
  return handleDelete(() => deleteProjectInfo(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportProjectInfo(queryForm))
}

const ProjectInfoAddModalRef = ref<InstanceType<typeof ProjectInfoAddModal>>()
// 新增
const onAdd = () => {
  ProjectInfoAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (record: ProjectInfoResp) => {
  ProjectInfoAddModalRef.value?.onUpdate(record.id)
}

const ProjectInfoDetailDrawerRef = ref<InstanceType<typeof ProjectInfoDetailDrawer>>()
// 详情
const onDetail = (record: ProjectInfoResp) => {
  ProjectInfoDetailDrawerRef.value?.onOpen(record.id)
}
</script>

<style scoped lang="scss"></style>
